﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication4.Models;

namespace MvcApplication4.Controllers
{
    [Authorize]
    public class ThreadController : Controller
    {
        private ForumsEntities2 db = new ForumsEntities2();

        //
        // GET: /Thread/

        public ActionResult Index()
        {
            var threads = db.Threads.Include("Category1").Include("User");
            return View(threads.ToList());
        }

        //
        // GET: /Thread/Details/5

        public ActionResult Details(long id = 0)
        {
            Thread thread = db.Threads.Single(t => t.ThreadId == id);
            if (thread == null)
            {
                return HttpNotFound();
            }
            return View(thread);
        }

        //
        // GET: /Thread/Create

        public ActionResult Create()
        {
            ViewBag.Category = new SelectList(db.Categories, "CategoryId", "Title");
            ViewBag.Creator = new SelectList(db.Users, "UserId", "UserName");
            return View();
        }

        //
        // POST: /Thread/Create

        [HttpPost]
        public ActionResult Create(Thread thread)
        {
            if (ModelState.IsValid)
            {
                db.Threads.AddObject(thread);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.Category = new SelectList(db.Categories, "CategoryId", "Title", thread.Category);
            ViewBag.Creator = new SelectList(db.Users, "UserId", "UserName", thread.Creator);
            return View(thread);
        }

        //
        // GET: /Thread/Edit/5

        public ActionResult Edit(long id = 0)
        {
            Thread thread = db.Threads.Single(t => t.ThreadId == id);
            if (thread == null)
            {
                return HttpNotFound();
            }
            ViewBag.Category = new SelectList(db.Categories, "CategoryId", "Title", thread.Category);
            ViewBag.Creator = new SelectList(db.Users, "UserId", "UserName", thread.Creator);
            return View(thread);
        }

        //
        // POST: /Thread/Edit/5

        [HttpPost]
        public ActionResult Edit(Thread thread)
        {
            if (ModelState.IsValid)
            {
                db.Threads.Attach(thread);
                db.ObjectStateManager.ChangeObjectState(thread, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.Category = new SelectList(db.Categories, "CategoryId", "Title", thread.Category);
            ViewBag.Creator = new SelectList(db.Users, "UserId", "UserName", thread.Creator);
            return View(thread);
        }

        //
        // GET: /Thread/Delete/5

        public ActionResult Delete(long id = 0)
        {
            Thread thread = db.Threads.Single(t => t.ThreadId == id);
            if (thread == null)
            {
                return HttpNotFound();
            }
            return View(thread);
        }

        //
        // POST: /Thread/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(long id)
        {
            Thread thread = db.Threads.Single(t => t.ThreadId == id);
            db.Threads.DeleteObject(thread);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult ReturnToCategory(long id)
        {
            return Redirect("../../Category/Details/" + id);
        }

        public ActionResult ContainedPosts(long id)
        {
            var categories = db.Posts.Where("it.Thread = " + id);
            return View("_Post_Partial", categories.ToList());
        }
    }
}